package com.aaa.controller;


import com.aaa.service.S_FrontTestService;
import com.aaa.util.EmpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/frontTest/")
public class S_FrontTestController {
    @Autowired
    private S_FrontTestService frontTestService;

    @RequestMapping("selQuestions")
    public String selQuestions(Model model){

        Integer exam_id=EmpUtil.getExam().getExam_id();
        //System.out.println(exam_id);
        //根据登录信息查询出随机生成的试题
        List<Map> testPaper = frontTestService.selQuestions(exam_id);
        String exam_name=frontTestService.selectExam(exam_id);
        model.addAttribute("testPaper",testPaper);
        System.out.println(testPaper);
        //model.addAttribute("exam_name",exam_name);

        return "homes/testPaper";
    }

    @RequestMapping("getAnswer")
    public String getAnswer(int[] papQuesId,int[] eid,String result) {
        //截取前台传过来的答案，组成数组
        String[] answer = result.split(",");
        Map map = new HashMap();
        //分数

        int score = 0;
        for (int i = 0; i < answer.length; i++) {
            //将参数取出存放进map中
            map.put("id", papQuesId[i]);
            map.put("answer", answer[i]);
            map.put("exam_id", eid[i]);
            int a=frontTestService.getAnswer(map);
            score+=a;
        }
        frontTestService.addScorce(eid[0],score);
        frontTestService.updPeople(EmpUtil.getExam().getId());
        return "homes/hns";
    }


}
